/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.system.auth.sessionmanager.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import cn.com.ksplatform.core.context.PlatformContext;
import cn.com.ksplatform.modules.system.auth.loginmanager.controller.LoginCons;
import cn.com.ksplatform.modules.system.auth.sessionmanager.model.OnlineModel;
import cn.com.ksplatform.modules.system.auth.sessionmanager.store.OnlineClients;
import cn.com.ksplatform.modules.system.auth.sessionmanager.util.SessionOnlineUtil;

/**
 * ClassName:SessionListener.java <br/>
 * Date: 2015年12月4日 <br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@WebListener
public class SessionListener implements HttpSessionAttributeListener{
	
	@Override
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		if(LoginCons.LOGIN_USER.equals(arg0.getName())){
			OnlineModel om = SessionOnlineUtil.getOnlineModel(arg0.getSession());
			PlatformContext.getBean(OnlineClients.class).onlines.add(om);
		}
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		if(LoginCons.LOGIN_USER.equals(arg0.getName())){
			OnlineModel om = new OnlineModel();
			om.setSessionId(arg0.getSession().getId());
			PlatformContext.getBean(OnlineClients.class).onlines.remove(om);
		}
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		if(LoginCons.LOGIN_USER.equals(arg0.getName())){
			attributeAdded(arg0);
		}
	}

}
